﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CommonsLib.Utility;
using System.Data.SqlClient;
using MaxLeaf.Platform.Functions;
using System.Data.Common;
using MaxLeaf.Platform.Commons;
using MaxLeaf.Platform.DataContract;
using CommonsLib.DB;
using System.Data;

namespace MaxLeaf.Platform.DALImplementation
{
    public class poOrderHdrQryDAL : BaseDAL
    {
        protected override KeyValuePair<string, IDataParameter[]> GetCond(DBAccess dbcon, object model, List<Cond> lstCond)
        {
            KeyValuePair<string, IDataParameter[]> kvp = base.GetCond(dbcon, model, lstCond);
            string strKey = kvp.Key;
            foreach (Cond cond in lstCond)
            {
                if (cond.PropName.Equals("Func_GetCompanyBill"))
                {
                    if (!string.IsNullOrEmpty(kvp.Key))
                    {
                        strKey += " and ";
                    }
                    strKey += string.Format(@" ( 
                        (OrigCompanyID<>0  and OrigCompanyID={0}) 
                        or   ( CompanyID={0})
                        or exists(select a.CompanyID  from {1}mCompany  a where a.ParentId={0} and a.ParentId<>0  and a.CompanyID=case when poOrderHdr.OrigCompanyID<>0 then poOrderHdr.OrigCompanyID else poOrderHdr.CompanyID end) ) 
                    ", cond.Value, GetBaseDatabase());
                    continue;
                }

            }
            return new KeyValuePair<string,IDataParameter[]>(strKey,kvp.Value);
        }

        public override string GetTableName(object model)
        {
            return poOrderHdrDAL.QryTableName(model);
        }
    }
}
